package com.lwl.service.impl.mht;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwl.entity.MyPage;
import com.lwl.entity.mht.TMhtCallback;
import com.lwl.entity.mht.TMhtRecord;
import com.lwl.entity.mht.Charact;
import com.lwl.entity.mht.Statistics1;
import com.lwl.entity.mht.Statistics2;
import com.lwl.service.mht.TMhtRecordService;
import com.lwl.dao.mht.TMhtRecordMapper;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (TMhtRecord)表服务实现类
 * @author Lei
 * @since 2022-10-28 19:21:00
 */
@Service
public class TMhtRecordServiceImpl implements TMhtRecordService {
   @Autowired
   private TMhtRecordMapper tMhtRecordDao;

   @Override
   public PageInfo queryAll(MyPage myPage, TMhtRecord tMhtRecord) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      List<TMhtRecord> tMhtRecords = tMhtRecordDao.queryAll(tMhtRecord);
//      System.out.println("查到的结果：tMhtRecords = " + tMhtRecords);
      String charact = "";
//      将性格特点的属性拿出来，赋值给classifyAll属性
      for (TMhtRecord mhtRecord : tMhtRecords) {
//         一对多查询后，将特点数组中的每个心理特点都拿出来，当做字符串的一部分，如果对应的心里特点字符串长度为0，则就是没做题
         if (mhtRecord.getCharactList().size()!=0){
            String assessment = mhtRecord.getCharactList().get(0).getAssessment();
            String study = mhtRecord.getCharactList().get(0).getStudy();
            String friend = mhtRecord.getCharactList().get(0).getFriend();
            String lonely = mhtRecord.getCharactList().get(0).getLonely();
            String guiltily = mhtRecord.getCharactList().get(0).getGuiltily();
            String allergy = mhtRecord.getCharactList().get(0).getAllergy();
            String body = mhtRecord.getCharactList().get(0).getBody();
            String terror = mhtRecord.getCharactList().get(0).getTerror();
            String impulsion = mhtRecord.getCharactList().get(0).getImpulsion();
            String classify = mhtRecord.getCharactList().get(0).getClassify();
            charact += assessment+"、"+study+"、"+friend+"、"+lonely+"、"+guiltily+"、"+allergy+"、"+body+"、"+terror+"、"+impulsion+"、"+classify;
            mhtRecord.setClassifyAll(charact);
//            赋值完上一个之后，将字符串清空
            charact = "";
         }else {
            String NoWork = "未做题";
            mhtRecord.setClassifyAll(NoWork);
         }
      }
      PageInfo pageInfo = new PageInfo(tMhtRecords);
      return pageInfo;
   }

   @Override
   public PageInfo getRecordDetails(MyPage myPage, Integer id) {
      PageHelper.startPage(myPage.getPage(), myPage.getLimit());
//      这里是为了接收查询结果，因为查询结果并不在同一个实体类中
      List<Map<String,Object>> details = tMhtRecordDao.getRecordDetails(id);
      PageInfo pageInfo = new PageInfo(details);
      return pageInfo;
   }

    @Override
    public TMhtRecord checkRecord(TMhtRecord tMhtRecord) {
        TMhtRecord tMhtRecord1 = tMhtRecordDao.selectOne(tMhtRecord);
       return tMhtRecord1;
    }

   @Override
   public int insertRecord(TMhtRecord tMhtRecord) {
      tMhtRecord.setCreatetime(new Date());
      int insert = tMhtRecordDao.insert(tMhtRecord);
//    要添加做题记录到数据库，并将添加的记录的id返回，因为在做完题目后添加做题记录详情时要用到该记录的id
      Example example = new Example(TMhtRecord.class);
      example.createCriteria().andEqualTo("tel",tMhtRecord.getTel());
      List<TMhtRecord> tMhtRecords = tMhtRecordDao.selectByExample(example);
      TMhtRecord tMhtRecord1 = tMhtRecords.get(0);
      System.out.println("刚才添加用户的id=========="+tMhtRecord1.getId());
      return tMhtRecord1.getId();
   }

   @Override
   public PageInfo getStatistics2(MyPage myPage, Statistics2 statistics2) {
      PageHelper.startPage(myPage.getPage(),myPage.getLimit());
      PageInfo pageInfo = new PageInfo(tMhtRecordDao.getStatistics2(statistics2));
      return pageInfo;
   }

   @Override
   public List<Statistics1> getStatistics1() {
      List<Statistics1> statistics1List = tMhtRecordDao.getStatistics1();
      return statistics1List;
   }

   @Override
   public boolean queryByTestId(Integer id) {
      TMhtRecord tMhtRecord = tMhtRecordDao.queryByTestId(id);
      System.out.println("查询到的记录结果为tMhtRecord = " + tMhtRecord);
      int size = tMhtRecord.getCharactList().size();
      System.out.println("查到该记录的长度为size = " + size);
      if (tMhtRecord.getCharactList().size()!=0){
         List<Charact> charactList = tMhtRecord.getCharactList();
//         满足所有条件都正常才算不用做调查回访
         if (charactList.get(0).getStudy().equals("正常")&&
               charactList.get(0).getFriend().equals("正常")&&
               charactList.get(0).getLonely().equals("正常")&&
               charactList.get(0).getGuiltily().equals("正常")&&
               charactList.get(0).getAllergy().equals("正常")&&
               charactList.get(0).getBody().equals("正常")&&
               charactList.get(0).getTerror().equals("正常")&&
               charactList.get(0).getImpulsion().equals("正常")
         ){
            return false;
         }else {
            return true;
         }
      }else {
         return true;
      }
   }


}
